import { createRouter, createWebHashHistory } from 'vue-router'
import routes from './routes'
import store from '../store'
import { Notify } from 'vant'

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

const checkList = ['/person', '/store', '/update'];
router.beforeEach(async (to, _, next) => {
    // 跳转到指定的地址 && vuex中没有存储登录者信息：才需要登录态校验
    let info = store.state.info;
    if (checkList.includes(to.path) && !info) {
        try {
            await store.dispatch('setInfoAsync');
        } catch (_) {
            // 没有登录
            Notify({ type: 'danger', message: '当前没有登录，请先登录!' });
            next(`/login?from=${to.path}`);
            return;
        }
    }
    next();
});

export default router